package com.aji.food1.controller;

import com.aji.allcommon.result.Result;
import com.aji.allcommon.service.FileStorageService;
import com.aji.allcommon.utills.MultipartFileUtill;
import com.aji.food1.domain.Food1;
import com.aji.food1.domain.Food1ConditionDto;
import com.aji.food1.domain.Sep;
import com.aji.food1.service.Food1Service;
import com.aji.food1.service.SepService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.List;

@CrossOrigin
@Api(tags = "食物现代相关接口")
@RestController
@RequestMapping("/food1")
@RequiredArgsConstructor
public class Food1Controller {
    private final Food1Service food1Service;
    private final SepService sepService;
    private final FileStorageService fileStorageService;
    @ApiOperation("dpt转url")
    @PostMapping("/dptToUrl")
    public Result dptToUrl(MultipartFile txt) throws IOException {
        String originalFilename = txt.getOriginalFilename();
        String simpFileName = originalFilename.substring(0, originalFilename.length() - 4) + ".txt";
        File filetxt1 = File.createTempFile("temp", null);
        txt.transferTo(filetxt1);
        FileInputStream txtInput = new FileInputStream(filetxt1);
        String txturl = fileStorageService.uploadFile("txt", simpFileName, txtInput, "text/plain");
        return Result.success(txturl);
    }
    @ApiOperation("查询 光谱 根据food1Id")
    @GetMapping("/food1Id/{food1Id}")
    public Result getSepByFood1Id(@PathVariable Integer food1Id){
        LambdaQueryWrapper<Sep> compoundLambdaQueryWrapper  =new LambdaQueryWrapper<Sep>().select()
                .eq(food1Id!=null,Sep::getFood1Id,food1Id);
        List<Sep> sepList = sepService.list(compoundLambdaQueryWrapper);
        return Result.success(sepList);

    }
    @ApiOperation("增加 需要忽略id")
    @PostMapping()
    public Result save(@RequestBody Food1 entity) throws IOException {
        food1Service.save(entity);
        //子表增加
        sepService.saveByUrl(entity);

        return Result.success();
    }
    @ApiOperation("删除")
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        //先删除子表
        sepService.removeByFood1Ids(ids);
        food1Service.removeByIds(ids);
        return Result.success();
    }
    @ApiOperation("修改")
    @PutMapping()
    public Result updateById(@RequestBody Food1 entity){
        System.out.println(entity);
        food1Service.updateById(entity);
        //先删除子表
        sepService.removeByFood1Ids(Collections.singletonList(entity.getId()));
        //添加子表
        sepService.saveByUrl(entity);
        return Result.success();
    }
    @ApiOperation("查询 根据id")
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        return Result.success(food1Service.getById(id));
    }
    @ApiOperation("查询 全部")
    @GetMapping("/list")
    public Result list(){
        return Result.success(food1Service.list(null));
    }
    @ApiOperation("条件分页查询")
    @GetMapping("/{page}/{pageSize}")
    public Result listByConditionAndPage(@PathVariable int page, @PathVariable int pageSize, Food1ConditionDto compound){
        //合法检验
        if(page<1||pageSize<1){return Result.error("分页参数异常");}
        Page<Food1> compoundPage= new Page<>(page,pageSize);
        LambdaQueryWrapper<Food1> compoundLambdaQueryWrapper  =new LambdaQueryWrapper<Food1>().select()
                .like(compound.getSamClass()!=null,Food1::getSamClass,compound.getSamClass())
                .like(compound.getProMode()!=null,Food1::getProMode,compound.getProMode())
                .ge(compound.getTemp1()!=null,Food1::getTemp,compound.getTemp1())
                .lt(compound.getTemp2()!=null,Food1::getTemp,compound.getTemp2())
                .ge(compound.getWater1()!=null,Food1::getWater,compound.getWater1())
                .lt(compound.getWater2()!=null,Food1::getWater,compound.getWater2())
                .ge(compound.getOldTime1()!=null,Food1::getOldTime,compound.getOldTime1())
                .lt(compound.getOldTime2()!=null,Food1::getOldTime,compound.getOldTime2());
        IPage<Food1> compoundIPage = food1Service.page(compoundPage, compoundLambdaQueryWrapper);
        return Result.success(compoundIPage);

    }

    @ApiOperation("获取数据总量")
    @GetMapping("/count")
    public Result count(){
        int count = Math.toIntExact(food1Service.count());
        return Result.success(count);
    }

}
